package com.dds.web.controller.system;

import java.util.List;
import java.util.Map;

import com.dds.common.core.domain.model.LoginUser;
import com.dds.common.utils.ServletUtils;
import com.dds.framework.web.service.TokenService;
import com.dds.system.domain.sys.SysConfig;
import com.dds.system.service.sys.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.common.utils.poi.ExcelUtil;
import com.dds.common.core.page.TableDataInfo;

/**
 * 系统属性Controller
 * 
 * @author gaolianjie
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController
{
    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询系统属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysConfig sysConfig)
    {
        startPage();
        List<Map<String, Object>> list = sysConfigService.selectSysConfigList(sysConfig);
        return getDataTable(list);
    }

    /**
     * 获取系统属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{attrKey}")
    public AjaxResult getInfo(@PathVariable("attrKey") String attrKey)
    {
        return AjaxResult.success(sysConfigService.selectSysConfigById(attrKey));
    }

    /**
     * 新增系统属性
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "系统属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysConfig sysConfig)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();

        return toAjax(sysConfigService.insertSysConfig(sysConfig, userId));
    }

    /**
     * 修改系统属性
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "系统属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysConfig sysConfig)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();

        return toAjax(sysConfigService.updateSysConfig(sysConfig, userId));
    }

    /**
     * 删除系统属性
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "系统属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attrKeys}")
    public AjaxResult remove(@PathVariable String[] attrKeys)
    {
        return toAjax(sysConfigService.deleteSysConfigByIds(attrKeys));
    }
}
